<?php

/**
 * SocialEnginePro
 *
 * @category   Application_Extensions
 * @package    tv
 * @author     Azim
 */

/**
 * @category   Application_Extensions
 * @package    tv
 */
class Tv_Form_Create extends Engine_Form {

    public function init() {
        $user_level = Engine_Api::_()->user()->getViewer()->level_id;
        $user = Engine_Api::_()->user()->getViewer();

        // Init form
        $this
                ->setTitle('Add New Tv')
                ->setDescription('')
                ->setAttrib('id', 'form-upload')
                ->setAttrib('name', 'tv_create')
                ->setAttrib('enctype', 'multipart/form-data')
                ->setAction(Zend_Controller_Front::getInstance()->getRouter()->assemble(array()))
        ;



        // Init name
        $this->addElement('Text', 'title', array(
            'label' => 'Tv Name',
            'maxlength' => '40',
            'filters' => array(
                //new Engine_Filter_HtmlSpecialChars(),
                'StripTags',
                new Engine_Filter_Censor(),
                new Engine_Filter_StringLength(array('max' => '63')),
            ),
            'allowEmpty' => false,
            'required' => true,
            'validators' => array(
                array('NotEmpty', true)
            ),
        ));



        // Init descriptions
        $this->addElement('Textarea', 'description', array(
            'label' => 'Description',
            'filters' => array(
                'StripTags',
                new Engine_Filter_Censor(),
                //new Engine_Filter_HtmlSpecialChars(),
                new Engine_Filter_EnableLinks(),
            ),
        ));

        $this->addElement('file', 'photo', array(
            'label' => 'Photo',
            'validators' => array(
                array('Extension', false, 'jpeg, jpg, png'))
        ));
        
//        $this->addElement('text', 'uri', array(
//            'label'=>'url'
//        ));

        // Start time
        $start = new Engine_Form_Element_CalendarDateTime('starttime');
        $start->setLabel("Start Date");
        //$start->setValue(date('Y-m-d H:i:s'));
        //$start->setAllowEmpty(false);
        $this->addElement($start);

        $money = Engine_Api::_()->getDbtable('modules', 'core')->isModuleEnabled('money');

        if ($money) {
            $this->addElement('Radio', 'state', array(
                'label' => 'Status',
                // 'order' => '2',
                'multiOptions' => (array(
            '0' => 'Free',
            '1' => 'Paid'
                )),
                'value' => 0,
                'onchange' => 'updateForm(this.value)'
            ));

            $this->addElement('text', 'amount', array(
                'label' => 'Amount',
                    //'order' => '4'
            ));
        }

        $categoryTable = Engine_Api::_()->getDbtable('categories', 'tv');
        $categories = $categoryTable->getCategories();

        if (count($categories) != 0) {
            $categories_prepared = array('', '');
            foreach ($categories as $category) {
                $categories_prepared[$category->category_id] = $category->category_name;
            }

            // category field
            $this->addElement('Select', 'category_id', array(
                'label' => 'Category',
                'multiOptions' => $categories_prepared,
                'allowEmpty' => false,
                'required' => true,
                'validators' => array(
                    array('NotEmpty', true)
                ),
            ));
        }


        $localeObject = Zend_Registry::get('Locale');
        
        
        $languages = Zend_Locale::getTranslationList('language', $localeObject);
        $territories = Zend_Locale::getTranslationList('territory', $localeObject);

        $localeMultiOptions = array();
    
        $this->addElement('Select', 'language', array(
            'label' => 'Language',
            'multiOptions' => $languages,
            'value'=>'en'
        ));


        asort($territories);

        $this->addElement('Select', 'country', array(
            'label' => 'Country',
            'multiOptions' => $territories,
        ));

        //ADD AUTH STUFF HERE

        $availableLabels = array(
            'everyone' => 'Everyone',
            'registered' => 'All Registered Members',
            'owner_network' => 'Friends and Networks',
            'owner_member_member' => 'Friends of Friends',
            'owner_member' => 'Friends Only',
            'owner' => 'Just Me'
        );

        // Init search
        $this->addElement('Checkbox', 'search', array(
            'label' => Zend_Registry::get('Zend_Translate')->_("Show this tv in search results"),
            'value' => 1,
            'disableTranslator' => true
        ));

        // Element: auth_view
        $viewOptions = (array) Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('tv', $user, 'auth_view');
        $viewOptions = array_intersect_key($availableLabels, array_flip($viewOptions));

        if (!empty($viewOptions) && count($viewOptions) >= 1) {
            // Make a hidden field
            if (count($viewOptions) == 1) {
                $this->addElement('hidden', 'auth_view', array('value' => key($viewOptions)));
                // Make select box
            } else {
                $this->addElement('Select', 'auth_view', array(
                    'label' => 'Privacy',
                    'description' => 'Who may see this tv?',
                    'multiOptions' => $viewOptions,
                    'value' => key($viewOptions),
                ));
                $this->auth_view->getDecorator('Description')->setOption('placement', 'append');
            }
        }

        // Element: auth_comment
        $commentOptions = (array) Engine_Api::_()->authorization()->getAdapter('levels')->getAllowed('tv', $user, 'auth_comment');
        $commentOptions = array_intersect_key($availableLabels, array_flip($commentOptions));

        if (!empty($commentOptions) && count($commentOptions) >= 1) {
            // Make a hidden field
            if (count($commentOptions) == 1) {
                $this->addElement('hidden', 'auth_comment', array('value' => key($commentOptions)));
                // Make select box
            } else {
                $this->addElement('Select', 'auth_comment', array(
                    'label' => 'Comment Privacy',
                    'description' => 'Who may post comments on this tv?',
                    'multiOptions' => $commentOptions,
                    'value' => key($commentOptions),
                ));
                $this->auth_comment->getDecorator('Description')->setOption('placement', 'append');
            }
        }

        // Init video
        $this->addElement('Select', 'type', array(
            'label' => 'Video Source',
            'multiOptions' => array('0' => ' '),
            'onchange' => "updateTextFields()",
        ));

        //YouTube, Vimeo
        $video_options = Array();
        $video_options[1] = "YouTube";
        $video_options[2] = "Vimeo";
        $video_options[3] = "iframe";
        $video_options[4] = "stream";

        $this->type->addMultiOptions($video_options);

        // Init url
        $this->addElement('Text', 'url', array(
            'label' => 'Video Link (URL)',
            'description' => 'Paste the web address of the video here.',
            'maxlength' => '50'
        ));
        $this->url->getDecorator("Description")->setOption("placement", "append");

        // Init iframe

        $this->addElement('textarea', 'body', array(
            'label' => 'Body',
            'description' => 'Paste the code.'
        ));
        $this->body->getDecorator('Description')->setOption('placement', 'append');

        $this->addElement('Hidden', 'code', array(
            'order' => 1
        ));
        $this->addElement('Hidden', 'ignore', array(
            'order' => 2
        ));

        $this->addElement('Button', 'upload', array(
            'label' => 'Save',
            'type' => 'submit',
        ));
    }

    public function saveValues() {
        
    }

}
